import request from "../axios";

/**
 * 获取新的商品信息
 * @param {String} skuId - 商品SKUID
 * @returns Promise
 */
export const getNewCartGoods = (skuId) => {
  return request({
    url: `/goods/stock/${skuId}`,
    method: 'get',
    data: '',
  });
}

/**
 * 加入购物车
 * @param {String} skuId - 商品SKUID
 * @param {Integer} count - 商品数量
 * @returns Promise
 */
export const insertCart = ({ skuId, count }) => {
  return request({
    url: `/member/cart`,
    method: 'post',
    data: { skuId, count },
  });
}

/**
 * 获取商品的specs和skus
 * @param {String} skuId - 商品SKUID
 * @returns Promise
 */
export const getGoodsSku = (skuId) => {
  return request({
    url: `/goods/sku/${skuId}`,
    method: 'get',
    data: '',
  })
}

/**
 * 
 * 合并购物车
 * @param {Array<object>} cartList
 * @param {String} object.skuId
 * @param {String} object.selected
 * @param {String} object.count
 */
export const mergeCart = (cartList) => {
  return request({
    url: `/member/cart/merge`,
    method: 'post',
    data: cartList,
  })
}


/**
 * 获取购物车列表
 */
export const findCart = () => {
  return request({
    url: `/member/cart`,
    method: 'get',
  })
}

/**
 * 删除商品（支持批量删除）
 * @param {Array<string>} ids - skuId集合
 * @returns Promise
 */
export const deleteCart = (ids) => {
  return request({
    url: `/member/cart`,
    method: 'delete',
    data: { ids }
  })
}

/**
 * 修改购物车商品的状态和数量
 * @param {String} goods.skuId - 商品sku
 * @param {Boolean} goods.selected - 选中状态 
 * @param {Integer} goods.count - 商品数量
 * @returns Promise
 */
export const updateCart = (goods) => {
  return request({
    url: `/member/cart/${goods.skuId}`,
    method: 'put',
    data: goods
  })
}

/**
 * 全选反选
 * @param {Boolean} selected - 选中状态
 * @param {Array<string>} ids - 有效商品skuId集合
 * @returns Promise
 */
export const checkAllCart = ({ selected, ids }) => {
  return request({
    url: `/member/cart/selected`,
    method: 'put',
    data: { selected, ids }
  })
}